import { GBox } from "../../../../com/mui/containers/GBox";
import { GMarketButtonItem } from "./GMarketButtonItem";
import { InvalidationType } from "../../../../fl/core/InvalidationType";
import { EventName } from "../../mvc/EventName";
import { MuiEvent } from "../../../../com/mui/events/MuiEvent";
import { DataEvent } from "../../events/DataEvent";
import { GBoxDirection } from "../../../../com/mui/containers/globalVariable/GBoxDirection";
import { Vector } from "../../../../__AS3__/vec/Vector";
type int = number;
//class GMarketButtonBar
    
    export  class GMarketButtonBar extends GBox
    {
        constructor()
        {
            super();
            this.allButton = new Vector.<GMarketButtonItem>();
            return;
        }

        public set dataProvider(arg1: Array<any>)
        {
            if (this._dataProvider == arg1) 
            {
                return;
            }
            this._dataProvider = arg1;
            this._dataProviderChange = true;
            invalidate(InvalidationType.ALL);
            return;
        }

        public get dataProvider(): Array<any>
        {
            return this._dataProvider;
        }

        public set selectedIndex(arg1: int)
        {
            if (this._selectedIndex != arg1) 
            {
                this._selectedIndex = arg1;
                invalidate();
            }
            return;
        }

        public get selectedIndex(): int
        {
            return this._selectedIndex;
        }

        protected /* override */ updateDisplayList(): void
        {
            var loc1=null;
            var loc2=null;
            if (this._dataProviderChange) 
            {
                this.reset();
                var loc3=0;
                var loc4=this._dataProvider;
                for(loc2 of loc4) 
                {
                    loc1 = new GMarketButtonItem(loc2.name);
                    loc1.isClose = true;
                    loc1.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonItemClickHandler);
                    addChild(loc1);
                    this.allButton.push(loc1);
                }
                this._dataProviderChange = false;
            }
            if (this._selectedIndex > -1) 
            {
                this.checkSelected();
            }
            super.updateDisplayList();
            dispatchEvent(new Object /* flash.events.Event */(EventName.RankBarInitEnd));
            return;
        }

        /* internal  */checkSelected(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < numChildren) 
            {
                loc1 = getChildAt(loc2) as GMarketButtonItem;
                if (this.selectedIndex != loc2) 
                {
                    if (!loc1.isClose) 
                    {
                        loc1.isClose = true;
                    }
                }
                else 
                {
                    loc1.isClose = false;
                    dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, this.selectedIndex));
                }
                ++loc2;
            }
            return;
        }

        /* internal  */onButtonItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as GMarketButtonItem;
            var loc2=getChildIndex(loc1);
            if (loc1.isClose) 
            {
                loc1.isClose = false;
                dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, loc2));
            }
            else 
            {
                loc1.isClose = true;
                dispatchEvent(new DataEvent(EventName.MarketButtonClose, loc2));
            }
            if (!(this._selectedIndex == loc2) && this._selectedIndex > -1) 
            {
                (getChildAt(this._selectedIndex) as GMarketButtonItem).isClose = true;
            }
            this._selectedIndex = loc2;
            return;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            this.reset();
            super.dispose(arg1);
            return;
        }

        public resetButtonPos(arg1: int, arg2: int): void
        {
            var loc1=0;
            var loc4=null;
            var loc2=0;
            var loc3=this.numChildren;
            if (this.direction == GBoxDirection.VERTICAL) 
            {
                loc1 = 0;
                while (loc1 < loc3) 
                {
                    (loc4 = this.getChildAt(loc1)).x = 0;
                    loc4.y = loc2 + verticalGap;
                    if (loc1 != arg1) 
                    {
                        loc2 = loc4.y + loc4.height;
                    }
                    else 
                    {
                        loc2 = loc4.y + loc4.height + arg2;
                    }
                    ++loc1;
                }
            }
            return;
        }

        public getFirstBlankPos(arg1: int): Object /* flash.geom.Point */
        {
            var loc3=null;
            var loc1=0;
            var loc2=0;
            if (arg1 > -1) 
            {
                if (loc3 = getChildAt(arg1) as GMarketButtonItem) 
                {
                    loc1 = loc3.x;
                    loc2 = loc3.y + loc3.height;
                }
            }
            return new Object /* flash.geom.Point */(loc1, loc2);
        }

        protected reset(): void
        {
            this.height = 0;
            this._selectedIndex = -1;
            this.allButton = new Vector.<GMarketButtonItem>();
            while (numChildren > 0) 
            {
                (getChildAt(0) as GMarketButtonItem).removeEventListener(flash.events.MouseEvent.CLICK, this.onButtonItemClickHandler);
                removeChild(getChildAt(0));
            }
            return;
        }

        public clear(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this.allButton.length) 
            {
                loc1 = this.allButton[loc2] as GMarketButtonItem;
                loc1.isClose = true;
                ++loc2;
            }
            return;
        }

        /* internal */ /* var */_dataProvider: Array<any>;

        /* internal */ /* var */_dataProviderChange: Boolean=false;

        /* internal */ /* var */_selectedIndex: int=-1;

        protected /* var */allButton: Vector.<GMarketButtonItem>;
    }
